﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SuYuanRabbit.ErrorCode;
using SuYuanRabbit.ReadAPI.Applications.Commands;
using SuYuanRabbit.ReadAPI.Applications.Commands.Login;
using SuYuanRabbit.ReadAPI.Applications.Commands.Shops;
using SuYuanRabbit.ReadAPI.Applications.Commands.Users;
using SuYuanRabbit.ReadAPI.Dtos.Request;
using SuYuanRabbit.ReadAPI.Dtos.Response;

namespace SuYuanRabbit.ReadAPI.Controllers
{
    /// <summary>
    /// 溯源管理系统读的API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceabilityManagementReadController : ControllerBase
    {
        
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        public TraceabilityManagementReadController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 异步登录
        /// </summary>
        /// <param name="dto">请求</param>
        /// <returns>返回任务</returns>
        [HttpPost("LoginAsync")]
        public async Task<ApiResult<UserDto>> LoginAsync(LoginReqeustDto dto)
        {
            return await _mediator.Send(new LoginCommandHander { Account = dto.Account, Password = dto.Password });
        }
        #region 溯源信息管理
        /// <summary>
        /// 异步查询商品
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <returns>返回任务</returns>
        [HttpGet("QueryShopAsync")]
        public async Task<PaggingResult<ShopsDto>> QueryShopAsync(int pageIndex,int pageSize)
        {
            return await _mediator.Send(new QueryShopsCommand { PageIndex =pageIndex, PageSize = pageSize });
        }
        #endregion
        #region RBAC管理
        /// <summary>
        /// 查询所有用户信息【支持分页及条件检索】
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">每页数量</param>
        /// <param name="userNickName">用户昵称</param>
        /// <param name="userPhone">用户手机号</param>
        /// <returns>返回任务</returns>
        [HttpGet("QueryUserInfoAsync")]
        public async Task<PaggingResult<QueryUserDto>> QueryUserInfoAsync(int pageIndex,int pageSize,string? userNickName,string? userPhone)
        {
            return await _mediator.Send(new QueryUserCommand 
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                UserNickName = userNickName,
                UserPhone = userPhone,
            });
        }
        #endregion
    }
}
